//#pragma once
#ifndef __SAMPLESCONVERT_H__
#define __SAMPLESCONVERT_H__
#include <stdlib.h>

inline long C2F(char* cbuf,float* fbuf1,float* fbuf2,long n)
{
	float rational=1.0/128.0;
	if(cbuf==NULL||fbuf1==NULL||fbuf2==NULL||n<0)
	{
		return -1;
	}
	int i;
	try{
		for(i=0;i<n;++i)
		{
			fbuf1[i]=float(cbuf[(i<<1)])*rational;
			fbuf2[i]=float(cbuf[(i<<1)+1])*rational;
		}
	}
	catch(...)
	{
		return i;
	}
	return n;
}

inline long F2C(char* cbuf,float* fbuf1,float* fbuf2,long n)
{
	if(cbuf==NULL||fbuf1==NULL||fbuf2==NULL||n<0)
	{
		return -1;
	}
	int i;
	try{
		for(i=0;i<n;++i)
		{
			cbuf[(i<<1)]=char(fbuf1[i]*128.0f);
			cbuf[(i<<1)+1]=char(fbuf2[i]*128.0f);
		}
	}
	catch(...)
	{
		return i;
	}
	return n;
}

inline long S2F(short* sbuf,float* fbuf1,float* fbuf2,long n)
{
	float rational=1.0/32767.0;
	if(sbuf==NULL||fbuf1==NULL||fbuf2==NULL||n<0)
	{
		return -1;
	}
	int i;
	try{
		for(i=0;i<n;++i)
		{
			fbuf1[i]=float(sbuf[(i<<1)])*rational;
			fbuf2[i]=float(sbuf[(i<<1)+1])*rational;
		}
	}
	catch(...)
	{
		return i;
	}
	return n;
}

inline long F2S(short* sbuf,float* fbuf1,float* fbuf2,long n)
{
	if(sbuf==NULL||fbuf1==NULL||fbuf2==NULL||n<0)
	{
		return -1;
	}
	int i;
	try{
		for(i=0;i<n;++i)
		{
			sbuf[(i<<1)]=short(fbuf1[i]*32767.0f);
			sbuf[(i<<1)+1]=short(fbuf2[i]*32767.0f);
		}
	}
	catch(...)
	{
		return i;
	}
	return n;
}

inline long Fn2F(float* fbuf,float* fbuf1,float* fbuf2,long n)
{
	if(fbuf==NULL||fbuf1==NULL||fbuf2==NULL||n<0)
	{
		return -1;
	}
	int i;
	try{
		for(i=0;i<n;++i)
		{
			fbuf1[i]=fbuf[(i<<1)];
			fbuf2[i]=fbuf[(i<<1)+1];
		}
	}
	catch(...)
	{
		return i;
	}
	return n;
}

inline long F2Fn(float* fbuf,float* fbuf1,float* fbuf2,long n)
{
	if(fbuf==NULL||fbuf1==NULL||fbuf2==NULL||n<0)
	{
		return -1;
	}
	int i;
	try{
		for(i=0;i<n;++i)
		{
			fbuf[(i<<1)]=fbuf1[i];
			fbuf[(i<<1)+1]=fbuf2[i];
		}
	}
	catch(...)
	{
		return i;
	}
	return n;
}

#endif